package team18.yuejian.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import team18.yuejian.Activity.BaseActivity;
import team18.yuejian.Activity.MineListActivity;
import team18.yuejian.Activity.PicDetailActivity;
import team18.yuejian.Model.MineModel;
import team18.yuejian.Model.RetrofitModel;
import team18.yuejian.R;
import team18.yuejian.bean.PicBean;

import static android.view.View.GONE;

public class ShowPicAdapter extends RecyclerView.Adapter<ShowPicAdapter.ViewHolder> {
    private Context mcontext;
    private List<PicBean> mPicList;
    private int mflag;



    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView pic;
        TextView picIntro,delete;
        LinearLayout mlayout;

        public ViewHolder(View view){
            super(view);
            cardView = (CardView) view;
            pic = view.findViewById(R.id.cardview_pic);
            picIntro = view.findViewById(R.id.cardview_info);
            mlayout = view.findViewById(R.id.cardview_layout);
            delete = view.findViewById(R.id.cardview_delete);
        }
    }

    public ShowPicAdapter(List<PicBean> picList,int flag){
        mPicList = picList;
        mflag = flag;
    }

    @Override
    public ShowPicAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mcontext == null){
            mcontext = parent.getContext();
        }

        View view = LayoutInflater.from(mcontext).inflate(R.layout.cardview_list,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final PicBean picBean = mPicList.get(position);
        holder.picIntro.setText(picBean.getSpcontent());
        RetrofitModel retrofitModel = new RetrofitModel();
        String url = retrofitModel.getPicUrl();
        Glide.with(mcontext).load(url+"/showpic/"+picBean.getSppic()).into(holder.pic);

        holder.mlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mcontext,PicDetailActivity.class);
                intent.putExtra("spid",picBean.getSpid());
                mcontext.startActivity(intent);
            }
        });

        if (mflag==1){
            holder.delete.setVisibility(GONE);
        }else if (mflag==2){
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent1 = new Intent(mcontext,MineListActivity.class);
                    intent1.putExtra("type",3);
                    intent1.putExtra("delete",picBean.getSpid());
                    mcontext.startActivity(intent1);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mPicList.size();
    }
}
